package com.exercise.AndroidCamera;

import java.io.File;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;

import com.telpoo.frame.delegate.Idelegate;

public class MyUtils {

	public static String uploadImgur(String path, String clientId) {
		try {
			File file = new File(path);
			final String upload_to = "https://api.imgur.com/3/upload.json";
			String API_key = "27905d84c9ec40a";
			if (clientId != null)
				API_key = clientId;
			HttpClient httpClient = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost httpPost = new HttpPost(upload_to);
			httpPost.setHeader("Authorization", "Client-ID " + API_key);

			final MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);
			entity.addPart("image", new FileBody(file));
			entity.addPart("key", new StringBody(API_key));
			entity.addPart("description", new StringBody("test upimgae"));
			httpPost.setEntity(entity);
			final HttpResponse response = httpClient.execute(httpPost,
					localContext);
			final String response_string = EntityUtils.toString(response
					.getEntity());
			// return response_string;
			final JSONObject json = new JSONObject(response_string);
			String link = json.getJSONObject("data").getString("link");
			// link = "http://i.imgur.com/"+link+"l.png";
			return link;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	public static void dialog(Activity acti,String text,final Idelegate idelegate){
		AlertDialog.Builder builder=new AlertDialog.Builder(acti);
		builder.setMessage(text);
		AlertDialog dl = builder.create();
		dl.setOnDismissListener(new OnDismissListener() {
			
			@Override
			public void onDismiss(DialogInterface arg0) {
				idelegate.callBack("", 0);
				
			}
		});
		dl.show();
	}

}
